/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ilookinto.util;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import org.apache.jackrabbit.util.Text;

/**
 *
 * @author admin
 */
public class NodeUtil {

  public static Node emailNode(String emailFolder, Node root) {

    Node node = null;
    try {
      String spe = "special-folders";
      if (!root.hasNode(spe)) {
        node = root.addNode(spe, "nt:folder");
      } else {
        node = root.getNode(spe);
      }
      if (node.hasNode("emails")) {
        node = node.getNode("emails");
      } else {
        node = node.addNode("emails", "nt:folder");
      }
      if (node.hasNode(emailFolder)) {
        node = node.getNode(emailFolder);
      } else {
        node = node.addNode(emailFolder, "nt:folder");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return node;

  }

  public static Node fileNodeFromURL(String sUrl, Node root) throws Exception {
    // final URL currentURL = urls[i];
    String path = "";// urls[i].getPath();

    String host = "";
    if (!path.startsWith("http")) {
      path = sUrl;
    } else {
      URL url = new URL(sUrl);
      host = url.getHost();
    }
    List folderNames = new ArrayList();
    folderNames.addAll(Arrays.asList(host.split("[\\.|:]")));
    Collections.reverse(folderNames);
    folderNames.addAll(Arrays.asList(path.split("/|\\\\")));
    final String fileName = URLDecoder.decode((String) folderNames.remove(folderNames.size() - 1), "UTF-8").replaceAll(":", "_");
    Node node = root;
    for (Iterator fn = folderNames.iterator(); fn.hasNext();) {
      String name = URLDecoder.decode((String) fn.next(), "UTF-8");
      name = name.replaceAll(":", "_");
      if (name.length() == 0) {
        continue;
      }
      if (!node.hasNode(name)) {
        node.addNode(name, "nt:folder");
        //System.out.println("added name=" + name);
      }
      node = node.getNode(name);
    }
    final Node file;
    if (!node.hasNode(fileName)) {
      file = node.addNode(fileName, "nt:file");
    } else {
      file = node.getNode(fileName);
    }
    return file;
  }

  public static String guessContentTypeFromName(String fileName) {
    String mimeType = URLConnection.guessContentTypeFromName(fileName);
    if (mimeType == null) {
      if (fileName.endsWith(".doc") || fileName.endsWith(".docx")) {
        mimeType = "application/msword";
      } else if (fileName.endsWith(".xls")) {
        mimeType = "application/vnd.ms-excel";
      } else if (fileName.endsWith(".ppt")) {
        mimeType = "application/mspowerpoint";
      } else {
        mimeType = "application/octet-stream";
      }
    }
    return mimeType;
  }

  public static void main(String s[]) {
    //System.out.println(Arrays.asList(("c:/a/b").split("/|\\\\")));
    System.out.println("127.1.".replaceAll("\\.", "-"));
    System.out.println(Text.escapeIllegalJcrChars("127.1."));
    javax.activation.MimetypesFileTypeMap m = new javax.activation.MimetypesFileTypeMap();
    System.out.println(m.getContentType("abc.zip"));
    System.out.println(new org.apache.tika.mime.MimeTypes().getMimeType("abc.zip"));
     List folderNames = new ArrayList();
    folderNames.addAll(Arrays.asList("abc.cdf:jj".split("[\\.|:]")));
    System.out.println(Arrays.asList("abc.cdf:jj".split("[\\.|:]")));
    for(Object n: folderNames)
    {
      System.out.println(n.toString());
    }
    
    // MimeUtil mm = new MimeUtil();
    //Collection<MimeType> t = mm.getMimeTypes("abc.zip");
    //for (MimeType t1: t)
    //System.out.println(t1.getSubType());
  }
}
